/**
/ * @author Ramprasad Sivaprakasam (A0092629)
 */

package sg.edu.nus.iss.team4ft.ui;

import java.awt.BorderLayout;

import java.awt.Label;
import java.awt.Panel;

import javax.swing.JPanel;

import sg.edu.nus.iss.team4ft.service.CustomerAccount;

public class CustomerAccountRegistration extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private CustomerAccount customerAccount;
	private CustomerControl customerControl;
	private AccountControl accountControl;
	private AccountHistoryView accountHistoryView;

	public CustomerAccountRegistration(CustomerAccount customerAccount, Mode mode) throws CloneNotSupportedException, Exception{
		if(customerAccount == null){
			try{
				if (mode == Mode.New){
					this.customerAccount = new CustomerAccount();
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			
		}else{
			this.customerAccount = customerAccount;
		}
		
		loadControls(mode, this.customerAccount);		
	}

	private void loadControls(Mode mode, CustomerAccount customerAccount) throws CloneNotSupportedException, Exception {
		customerControl = new CustomerControl(mode, customerAccount);
		customerControl.setVisible(true);
		if(mode == Mode.View)
			accountHistoryView = new AccountHistoryView(customerAccount.getAccount());
		else
			accountControl = new AccountControl(mode, customerAccount.getAccount());
		Panel p = new Panel(new BorderLayout(0,1));
		Panel p1 = new Panel(new BorderLayout(0,1));
		
		Label c = new Label("Customer Information:");
		c.setFont(new java.awt.Font("Arial",1,12));
		p.add("North",c);
		p.add("West",new Panel());
		p.add("Center",customerControl);
		
		this.setLayout(new BorderLayout(0,1));
		this.add("West",p);
		this.add("Center",new Panel());
		
		Label l = new Label("Account Information:");
		l.setFont(new java.awt.Font("Arial",1,12));
		if(mode == Mode.View){
			p1.add("North",l);
			p1.add("West",accountHistoryView);
		}
		else{
			p1.add("North",l);
			p1.add("West",accountControl);
		}
		this.add("East",p1);
	}
	
	public boolean isValidToSave(){
		return false;
	}

	public CustomerAccount getCustomerAccount() throws Exception {
		this.customerAccount = customerControl.getCustomerAccount();
		this.customerAccount.setAccount(accountControl.getAccount());
		return customerAccount;
	}

	public void setCustomerAccount(CustomerAccount customerAccount) {
		this.customerAccount = customerAccount;
	}
	
	public CustomerControl getCustomerControl(){
		return this.customerControl;
	}
	
	public void setCustomerControl(CustomerControl customerControl){
		this.customerControl = customerControl;
	}
}
